#include "stdafx.h"
#include "Utilities/utilities.h" // if you use STL, please include this line AFTER all other include
#include "Object.h"
#include "Model.h"
#include "Texture.h"
#include "Shader.h"


Object::Object() 
	: m_bNeedUpdate(true)
{
	m_position	= Vector3(0.0f, 0.0f, 0.0f);
	m_rotation	= Vector3(0.0f, 0.0f, 0.0f);
	m_scale		= Vector3(1.0f, 1.0f, 1.0f);
}

void Object::SetModel(Model * model)
{
	m_model = model;
}

void Object::AddTexture(Texture * texture)
{
	m_textures.push_back(texture);
}

void Object::SetShader(Shader * shader)
{
	m_shader = shader;
}

void Object::SetPosition(Vector3 & vec)
{
	m_position = vec;
}

void Object::SetRotation(Vector3 & vec)
{
	m_rotation = vec;
}

void Object::SetScale(Vector3 & vec)
{
	m_scale = vec;
}

bool Object::IsBlend()
{
	return (m_shader->m_isBlend);
}

void Object::Init ()
{
	//Load Model and Texture
}

void Object::Draw ()
{
	m_shader->PrepareVariables(m_worldMat);
	m_model->Bind(m_shader);

	bool bMulti = (m_textures.size() > 1);
	for (unsigned int i = 0; i < m_textures.size(); i++)
	{
		m_textures.at(i)->Bind(m_shader, i, bMulti);
	}

	glDrawElements(GL_TRIANGLES, m_model->m_nIndices, GL_UNSIGNED_INT, 0);

	m_model->UnBind();
	for (unsigned int i = 0; i < m_textures.size(); i++)
	{
		m_textures.at(i)->UnBind();
	}
	m_shader->ToggleStates(false);
}

void Object::UpdateWorldMat ()
{
	if (m_bNeedUpdate)
	{
		Matrix S, T, Rz, Rx, Ry;
		S.SetScale(m_scale.x, m_scale.y, m_scale.z);
		T.SetTranslation(m_position.x, m_position.y, m_position.z);
		Rz.SetRotationZ(m_rotation.z);
		Rx.SetRotationX(m_rotation.x);
		Ry.SetRotationY(m_rotation.y);
		m_worldMat = S * Rz * Rx * Ry * T;

		m_bNeedUpdate = true;
	}
}

void Object::CleanUp()
{
	m_textures.clear();
}